package Form;

import Matter.Link;

public class LNode {
    public Object data;
    public LNode lchild,rchild;

    public LNode(Object data) {
        this(data,null,null);
    }

    public LNode(Object data,LNode lchild,LNode rchild){
        this.data=data;
        this.lchild=lchild;
        this.rchild=rchild;

    }
    public LNode getLchild(){
        return lchild;
    }
    public void setLchild(LNode lchild){
        this.lchild=lchild;
    }
    public LNode getRchild(){
        return rchild;
    }
    public void setRchild(LNode rchild){
        this.rchild=rchild;
    }

    //删除子树
    public void delete(Object no) {
        if (this.lchild != null && this.lchild.data == no) {
            this.lchild = null;
        }
        if (this.rchild != null && this.rchild.data == no) {
            this.rchild = null;
        }
        if (this.lchild != null) {
            this.lchild.delete(no);
        }
        if (this.rchild!=null){
            this.rchild.delete(no);
        }
    }

    //添加子树
    public void insert(Object roo,Link dat){
        if(this.lchild==null&&this.data==roo){
            this.lchild=dat.getRoot();
        }else if(this.rchild==null&&this.data==roo){
            this.rchild=dat.getRoot();
        }else if(this.lchild!=null&&this.rchild!=null&&this.data==roo){
            this.lchild=dat.getRoot();
        }else {
            if (this.data != roo && this.lchild != null) {
                this.lchild.insert(roo, dat);
            }
            if (this.data != roo && this.rchild != null) {
                this.rchild.insert(roo, dat);
            }
        }
    }
}
